package com.ylbh.modules.banner.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.activity.dto.GoodsListDTO;
import com.ylbh.common.pojo.banner.dto.BannerListDTO;
import com.ylbh.common.pojo.banner.dto.GoodsCommendAddDTO;
import com.ylbh.common.pojo.banner.dto.GoodsCommendDTO;
import com.ylbh.common.pojo.banner.dto.StartAdvertisingDTO;
import com.ylbh.common.pojo.goods.dto.GoodsClassDTO;
import com.ylbh.common.pojo.goods.vo.ActivitySelectGoodsListVO;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageDTO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.banner.entity.AppHomeBanner;
import com.ylbh.modules.banner.mapper.AppHomeBannerMapper;
import com.ylbh.modules.banner.service.AppHomeBannerService;
import com.ylbh.modules.goods.service.GoodsSpecService;
import com.ylbh.modules.outerchain.entity.ShoppingOuterChain;
import com.ylbh.modules.outerchain.mapper.ShoppingOuterChainMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * appBanner管理
 */
@Service
public class AppHomeBannerServiceImpl extends ServiceImpl<AppHomeBannerMapper, AppHomeBanner> implements AppHomeBannerService {

    @Autowired
    private AppHomeBannerMapper appHomeBannerMapper;

    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private ShoppingOuterChainMapper shoppingOuterChainMapper;

    @Value("${url.iconFileName}")
    private String iconFileName;

    /**
     * banner列表
     * @param bannerListDTO
     * @return
     */
    @Override
    public PageUtils queryBannerList(BannerListDTO bannerListDTO) {
        Page<Object> page = new Page<>(bannerListDTO.getPage(), bannerListDTO.getLimit());
        List<AppHomeBanner> list = appHomeBannerMapper.selectBannerList(bannerListDTO, page);
        return new PageUtils(new Page<AppHomeBanner>().setTotal(page.getTotal()).setRecords(list));
    }

    @Override
    public PageUtils queryBannerLists(BannerListDTO bannerListDTO) {
        Page<Object> page = new Page<>(bannerListDTO.getPage(), bannerListDTO.getLimit());
        List<AppHomeBanner> list = appHomeBannerMapper.selectBannerLists(bannerListDTO, page);
        return new PageUtils(new Page<AppHomeBanner>().setTotal(page.getTotal()).setRecords(list));
    }
    /**
     * banner详情
     * @param id
     * @return
     */
    @Override
    public AppHomeBanner getBannerById(Long id) {
        AppHomeBanner appHomeBanner = appHomeBannerMapper.selectBannerDetails(id);
        if (!StringUtils.isEmpty(appHomeBanner.getUrl())) {
            // 根据跳转链接获取id
            ShoppingOuterChain soc = shoppingOuterChainMapper.selectOuterChainIdByUrl(appHomeBanner.getUrl());
            if (null != soc) {
                appHomeBanner.setUrl(soc.getId() + "");
            }
        }
        return appHomeBanner;
    }

    /**
     * 修改banner
     * @param appHomeBanner
     */
    @Override
    public void modifyBannerById(AppHomeBanner appHomeBanner) {
//        if (null != appHomeBanner.getUrl() && !StringUtils.isEmpty(appHomeBanner.getUrl())) {
//            appHomeBanner.setUrl(shoppingOuterChainMapper.selectById(Long.parseLong(appHomeBanner.getUrl())).getOuterUrl());
//        }
        appHomeBannerMapper.updateBannerById(appHomeBanner);
    }

    /**
     * 新增banner
     * @param appHomeBanner
     */
    @Override
    public void addBanner(AppHomeBanner appHomeBanner) {
        appHomeBanner.setAddTime(new Date());
//        if (null != appHomeBanner.getUrl() && !StringUtils.isEmpty(appHomeBanner.getUrl())) {
//            appHomeBanner.setUrl(shoppingOuterChainMapper.selectById(Long.parseLong(appHomeBanner.getUrl())).getOuterUrl());
//        }
        appHomeBannerMapper.insertBanner(appHomeBanner);
    }

    /**
     * 删除banner
     * @param list
     */
    @Override
    public void removeBannerByIds(List<String> list) {
        appHomeBannerMapper.deleteBannerByIds(list);
    }

    /**
     * 上传banner图片
     * @param file
     * @return
     */
    @Override
    public String uploadBanner(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"banner");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/banner/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 通过id获取楼层信息
     * @param id
     * @return
     */
    @Override
    public AppHomeBanner getFloorById(Integer id) {
        return this.getById(id);
    }

    /**
     * 修改Banner图片显示状态
     * @param id
     * @param showStatus
     */
    @Override
    public void modifyShowStatus(Long id, Integer showStatus) {
        appHomeBannerMapper.updateShowStatus(id, showStatus);
    }

    /**
     * 专题管理列表
     * @param bannerListDTO
     * @return
     */
    @Override
    public PageUtils querySpecialList(BannerListDTO bannerListDTO) {
        Page<Object> page = new Page<>(bannerListDTO.getPage(), bannerListDTO.getLimit());
        List<AppHomeBanner> list = appHomeBannerMapper.selectSpecialList(bannerListDTO, page);
//        for (AppHomeBanner vo : list) {
//            if (StringUtils.isNotBlank(vo.getUrlData())) {
//                vo.setUrlData(String.valueOf(vo.getUrlData().split(",").length));
//            }
//        }
        return new PageUtils(new Page<AppHomeBanner>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 根据分辨率条件来上传图片
     * @param file
     * @param type
     * @return
     */
    @Override
    public String uploadAdvertisingMap(MultipartFile file, String type) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadAdvertisingMap(file,iconFileName, type);//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/" + iconFileName + "/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 新增启动广告
     * @param startAdvertisingDTO
     */
    @Override
    public void addStartAdvertising(StartAdvertisingDTO startAdvertisingDTO) {
        startAdvertisingDTO.setAddTime(new Date());
        startAdvertisingDTO.setBannerType(16);
        appHomeBannerMapper.insertStartAdvertising(startAdvertisingDTO);
    }

    /**
     * 修改启动广告
     * @param startAdvertisingDTO
     */
    @Override
    public void modifyStartAdvertisingById(StartAdvertisingDTO startAdvertisingDTO) {
        appHomeBannerMapper.updateStartAdvertisingById(startAdvertisingDTO);
    }

    /**
     * 启动广告详情
     * @param id
     */
    @Override
    public StartAdvertisingDTO getStartAdvertisingById(Long id) {
        return appHomeBannerMapper.selectStartAdvertisingById(id);
    }

    /**
     * 启动广告页列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryStartAdvertisingList(PageDTO pageDTO) {
        Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<StartAdvertisingDTO> list = appHomeBannerMapper.selectStartAdvertisingList(pageDTO.getKeywords(), page);
        for (StartAdvertisingDTO dto:list) {
            if (!StringUtils.isEmpty(dto.getUrlData())) {
                dto.setUrlData(dto.getUrlData().split(",")[0]);
            }
        }
        return new PageUtils(new Page<StartAdvertisingDTO>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 商品推荐详情
     * @param id
     * @return
     */
    @Override
    public AppHomeBanner getGoodsRecommendInfoById(Integer id) {
        AppHomeBanner appHomeBanner = appHomeBannerMapper.selectById(id);
        // 查询出所有绑定在分类推荐上的商品id
        List<String> list = appHomeBannerMapper.selectGoodsIdInfoByIdBannerId(appHomeBanner.getId());
        appHomeBanner.setUrlData(String.join(",", list));
        return appHomeBanner;
    }

    /**
     * 修改商品推荐详情
     * @param goodsCommendDTO
     */
    @Override
    public void modifyGoodsRecommendDetailsById(GoodsCommendDTO goodsCommendDTO) {
        appHomeBannerMapper.deleteGoodsCommendById(goodsCommendDTO.getId());
        goodsCommendDTO.setGoodsId(goodsCommendDTO.getUrlData().split(","));
        if (goodsCommendDTO.getGoodsId().length > 0) {
            Set set = new HashSet();
            for (int i = 0; i < goodsCommendDTO.getGoodsId().length; i++) {
                set.add(goodsCommendDTO.getGoodsId()[i]);
            }
            goodsCommendDTO.setGoodsId(set.toArray());
        }
        appHomeBannerMapper.updateGoodsCommendById(goodsCommendDTO);
        for (Object goodsId:goodsCommendDTO.getGoodsId()) {
            appHomeBannerMapper.insertGoodsCommend(goodsId.toString(), goodsCommendDTO.getId());
        }
    }

    /**
     * 新增商品推荐
     * @param appHomeBanner
     */
    @Override
    public void addGoodsCommend(AppHomeBanner appHomeBanner) {
        appHomeBanner.setBannerType("6");
        appHomeBanner.setAddTime(new Date());
        appHomeBanner.setShowStatus(0);
        Object[] goodsId = appHomeBanner.getUrlData().split(",");
        // 插入标签
        appHomeBannerMapper.insert(appHomeBanner);
        if (goodsId.length > 0) {
            Set set = new HashSet();
            for (int i = 0; i < goodsId.length; i++) {
                set.add(goodsId[i]);
            }
            goodsId = (set.toArray());
        }
        for (Object id:goodsId) {
            // 插入中间表信息
            if (null != id)
                appHomeBannerMapper.insertGoodsCommend(id.toString(), appHomeBanner.getId());
        }
    }

    /**
     * 商品推荐添加商品查询
     * @param goodsListDTO
     * @return
     */
    @Override
    public PageUtils queryNotAddedGoods(GoodsCommendAddDTO goodsListDTO) {
        Page<Integer> page = new Page<>(goodsListDTO.getPage(), goodsListDTO.getLimit());
        List<ActivitySelectGoodsListVO> goodsLists = appHomeBannerMapper.selectNotAddedGoods(goodsListDTO, page);
        for (ActivitySelectGoodsListVO goods:goodsLists) {
            String sku = goods.getSku();
            goods.setSku(goodsSpecService.sortOutSku(sku));
        }
        return new PageUtils(new Page<ActivitySelectGoodsListVO>().setTotal(page.getTotal()).setRecords(goodsLists));
    }

    /**
     * 首页底部菜单icon
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryBottomIconList(PageDTO pageDTO) {
        Page<Object> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<AppHomeBanner> appHomeBanner = appHomeBannerMapper.selectBottomIconList(page);
        return new PageUtils(new Page<AppHomeBanner>().setTotal(page.getTotal()).setRecords(appHomeBanner));
    }

    /**
     * 首页底部菜单icon图标上传
     * @param file
     * @param request
     * @return
     */
    @Override
    public String uploadBottomIcon(MultipartFile file, HttpServletRequest request) {
        String type = request.getParameter("type");
        String date = request.getParameter("date");
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadBottomIcon(file,iconFileName, type, date);//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/" + iconFileName + "/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * vip升级商品列表
     * @param dto
     * @return
     */
    @Override
    public PageUtils vipUpgradeGoodsList(BannerListDTO dto) {
        Page<Object> page = new Page<>(dto.getPage(), dto.getLimit());
        List<AppHomeBanner> list = appHomeBannerMapper.selectVipUpgradeGoodsList(page);
        return new PageUtils(new Page<AppHomeBanner>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 图片跳转分类
     * @return
     */
    @Override
    public List<GoodsClassDTO> queryOuterClassList() {
        return appHomeBannerMapper.selectOuterClassList();
    }
}
